package com.controller.api;

import com.biz.CustomerNotesBiz;
import com.biz.CustomerNotesLikeLogBiz;
import com.controller.backend.BaseController;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.cond.BaseCond;
import com.entity.model.CustomerNotes;
import com.entity.model.CustomerNotesLikeLog;
import com.entity.vo.CustomerNotesVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.util.webpage.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jc on 2019/11/16.
 */
@RestController("apiCustomerNotesController")
@RequestMapping("/api/customerNotes")
public class CustomerNotesController extends BaseController<CustomerNotes> {
    @Autowired
    private CustomerNotesBiz customerNotesBiz;

    @Autowired
    private CustomerNotesLikeLogBiz customerNotesLikeLogBiz;

    // 新增笔记 notesType 0:草稿 1:笔记
    @RequestMapping(value = "/addMyNotes")
    public Result addMyNotes(CustomerNotes customerNotes) {
        if (customerNotes.getCustomerId() == null) {
            return ResultGenerator.genFailResult("请登录");
        }
        customerNotesBiz.save(customerNotes);
        return ResultGenerator.genSuccessResult(customerNotes.getId());
    }

    // 编辑笔记 notesType 0:草稿 1:笔记
    @RequestMapping(value = "/saveMyNotes")
    public Result saveMyNotes(CustomerNotes customerNotes) {
        customerNotesBiz.update(customerNotes);
        return ResultGenerator.genSuccessResult(customerNotes.getId());
    }

    // 删除笔记
    @RequestMapping(value = "/delMyNotes")
    public Result delMyNotes(@RequestParam Integer notesId) {
        customerNotesBiz.deleteById(notesId);
        return ResultGenerator.genSuccessResult();
    }

    // 发布草稿
    @RequestMapping(value = "/releaseMyNotes")
    public Result releaseMyNotes(@RequestParam Integer id) {
        if (id == null) {
            return ResultGenerator.genFailResult("参数错误");
        }
        CustomerNotes customerNotes = customerNotesBiz.findById(id);
        customerNotes.setNotesType(1);
        customerNotesBiz.update(customerNotes);
        return ResultGenerator.genSuccessResult();
    }


    // 我的笔记列表
    @RequestMapping(value = "/myNotesList")
    public Result myNotesList(@RequestParam Integer customerId, @RequestParam Integer notesType, BaseCond bcond) {
        if (customerId == null) {
            return ResultGenerator.genFailResult("用户编号错误");
        } else if (notesType == null) {
            return ResultGenerator.genFailResult("笔记类型错误");
        } else {
            PageHelper.startPage(bcond.getPageNum(), bcond.getPageSize(), false);
            List<CustomerNotesVO> customerNotes = customerNotesBiz.myNotesList(customerId, notesType);
            for (Integer i = 0; i < customerNotes.size(); i++) {
                if (customerNotes.get(i).getImages() != null) {
                    customerNotes.get(i).setImageNum(customerNotes.get(i).getImages().split(",").length);
                    customerNotes.get(i).setImages(customerNotes.get(i).getImages().split(",")[0]);
                } else {
                    customerNotes.get(i).setImageNum(0);
                }
            }
            return ResultGenerator.genSuccessResult(customerNotes);
        }
    }

    // 笔记详情
    @RequestMapping(value = "/notesDetail")
    public Result notesDetail(@RequestParam Integer notesId, Integer customerId) {
        if (notesId == null) {
            return ResultGenerator.genFailResult("笔记编号不能为空");
        } else {
            CustomerNotes customerNotes = customerNotesBiz.getNotesDetail(notesId, customerId);
            return ResultGenerator.genSuccessResult(customerNotes);
        }
    }


    // 笔记大厅 type 0:最新发布  1:最多点赞
    @RequestMapping(value = "/notesList")
    public Result notesList(@RequestParam Integer type, Integer customerId, BaseCond bcond) {
        if (type == null) {
            return ResultGenerator.genFailResult("笔记类型不能为空");
        }
        PageHelper.startPage(bcond.getPageNum(), bcond.getPageSize(), false);
        List<CustomerNotesVO> customerNotes = customerNotesBiz.notesList(type, customerId);
        for (Integer i = 0; i < customerNotes.size(); i++) {
            if (customerNotes.get(i).getImages() != null) {
                customerNotes.get(i).setImageNum(customerNotes.get(i).getImages().split(",").length);
                customerNotes.get(i).setImages(customerNotes.get(i).getImages().split(",")[0]);
            } else {
                customerNotes.get(i).setImageNum(0);
            }
        }
        return ResultGenerator.genSuccessResult(customerNotes);
    }

    // 点赞 取赞
    @RequestMapping(value = "/hitLike")
    public Result hitLike(@RequestParam Integer customerId, @RequestParam Integer notesId) {
        Condition condition = new Condition(CustomerNotes.class);
        condition.createCriteria().andEqualTo("customerId", customerId).andEqualTo("id", notesId);
        List<CustomerNotesLikeLog> list = customerNotesLikeLogBiz.findByCondition(condition);
        if (list.size() > 0) {
            CustomerNotes customerNotes = customerNotesBiz.findById(notesId);
            customerNotes.setLikeNum(customerNotes.getLikeNum() - 1);

            customerNotesBiz.update(customerNotes);

            customerNotesLikeLogBiz.deleteById(list.get(0).getId());
        } else {

            CustomerNotes customerNotes = customerNotesBiz.findById(notesId);
            customerNotes.setLikeNum(customerNotes.getLikeNum() + 1);
            customerNotesBiz.update(customerNotes);


            CustomerNotesLikeLog customerNotesLikeLog = new CustomerNotesLikeLog();
            customerNotesLikeLog.setCustomerId(customerId);
            customerNotesLikeLog.setNotesId(notesId);
            customerNotesLikeLogBiz.save(customerNotesLikeLog);
        }
        return ResultGenerator.genSuccessResult();
    }
}
